package com.wydexcel.generate.process.context;

import com.alibaba.fastjson.JSONObject;
import com.wydexcel.generate.properties.ExcelAbstractSheetProperties;
import com.wydexcel.generate.properties.s.ExcelBass;
import com.wydexcel.generate.properties.s.ExcelSegmentProperties;
import com.wydexcel.generate.properties.s.ExcelWorkPlaceProperties;

import java.util.HashMap;
import java.util.Map;

public class ExcelContextAllContext {


    private final Map<String, ExcelBass> usingMap = new HashMap<>();

    public Map<String, ExcelBass> getUsingMap() {
        return usingMap;
    }

    private final Map<String, ExcelWorkPlaceProperties> propertiesMap = new HashMap<>();
    private ExcelWorkPlaceProperties excelWorkPlaceProperties = new ExcelWorkPlaceProperties();
    private final static ExcelContextAllContext allContext = new ExcelContextAllContext();

    public static final ExcelContextAllContext getInstance() {
        return allContext;
    }

    public ExcelContextAllContext parse(String excelProperties) {
        excelWorkPlaceProperties = JSONObject.parseObject(excelProperties, ExcelWorkPlaceProperties.class);
        parse(excelWorkPlaceProperties);
        return this;
    }

    private ExcelContextAllContext parse(ExcelWorkPlaceProperties placeProperties) {
        if (null == placeProperties || null == placeProperties.getId() || "".equals(placeProperties.getId())
                || null == placeProperties.getMap() || placeProperties.getMap().isEmpty()) {
            throw new IllegalArgumentException("excelProperties  is null or excelProperties's id is null or excelProperties's map is null");
        }
        ExcelWorkPlaceProperties oldPlaceProperties = propertiesMap.get(placeProperties.getId());
        if (null != oldPlaceProperties) {
            // 为了多次赋值 将新的值覆盖到原来的map中
            Map<String, ExcelAbstractSheetProperties> map = placeProperties.getMap();
            oldPlaceProperties.getMap().forEach(map::put);
            return this;
        }
        propertiesMap.put(placeProperties.getId(), placeProperties);
        return this;
    }

    private ExcelContext excelContext;

    public ExcelContext parseComplete() {
        return excelContext = new ExcelContext(excelWorkPlaceProperties);
    }

    public void close() {
        excelContext.colse();
        excelContext = null;
    }


    public void partParse(String excelProperties) {
        ExcelSegmentProperties excelSegmentProperties = JSONObject.parseObject(excelProperties, ExcelSegmentProperties.class);
        if (null == excelSegmentProperties || null == excelSegmentProperties.getId() || "".equals(excelSegmentProperties.getId())
                || null == excelSegmentProperties.getSheetName() || "".equals(excelSegmentProperties.getSheetName())) {
            throw new IllegalArgumentException("SegmentProperties  is null or SegmentProperties's id is null or SegmentProperties's sheetName is null");
        }
        ExcelBass excelBass = null;

//        if (null == (excelBass = excelSegmentProperties.getExcelBass())) {
//            throw new IllegalArgumentException("SegmentProperties's base is null");
//        }

        ExcelWorkPlaceProperties placeProperties = propertiesMap.get(excelSegmentProperties.getId());
        if (null == placeProperties) {
            throw new IllegalArgumentException("id " + excelSegmentProperties.getId() + "  has no excelProperties");
        }
        String fieldName = excelBass.getFieldName();
        //拿到对应的sheet配置
        ExcelAbstractSheetProperties sheetProperties = placeProperties.getMap().get(excelSegmentProperties.getSheetName());
        //
        boolean first = sheetProperties.getCells().stream().anyMatch(filed -> filed.getExcelFieldName().equals(fieldName));
        if (!first) {
//            sheetProperties.getCells().add();
        }


    }

}
